package org.iteam.xlz.manager.listener;

import java.util.Calendar;
import java.util.Timer;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.iteam.xlz.manager.timer.ImagesTimerTask;
import org.iteam.xlz.manager.timer.ProductStatusTimerTask;
import org.iteam.xlz.manager.timer.RelationTimerTask;
import org.iteam.xlz.manager.timer.UmengTimerTask;

public class InitListener implements ServletContextListener {
	Timer umengTimer=new Timer();
	Timer imageTimer=new Timer();
	Timer relationTimer=new Timer();
	Timer productTimer=new Timer();
	@Override
	public void contextDestroyed(ServletContextEvent arg0) {
		umengTimer.cancel();
		System.out.println("已取消定时任务:umengTimer。。。");
		imageTimer.cancel();
		System.out.println("已取消定时任务:imageTimer。。。");
		relationTimer.cancel();
		System.out.println("已取消定时任务:RelationTimerTask。。。");
	}

	@Override
	public void contextInitialized(ServletContextEvent event) {
		String path=event.getServletContext().getRealPath("/");
		Calendar c=Calendar.getInstance();
		long now=c.getTime().getTime();
		c.add(Calendar.DAY_OF_MONTH,1);
		c.set(Calendar.HOUR_OF_DAY,0);
		c.set(Calendar.MINUTE,0);
		c.set(Calendar.SECOND,0);
		c.set(Calendar.MILLISECOND,0);
		long delay=c.getTime().getTime()-now;
		umengTimer.schedule(new UmengTimerTask(),0,1*60*1000);
		productTimer.schedule(new ProductStatusTimerTask(),0,1*60*1000);
		imageTimer.schedule(new ImagesTimerTask(path),delay,24*60*60*1000);
		relationTimer.schedule(new RelationTimerTask(),10*1000,1000*60*10);
	}
	
	public static void main(String[] args) {
		Calendar c=Calendar.getInstance();
		System.out.println(c.get(Calendar.MILLISECOND));
	}
}
